<template>
  <van-row>
    <van-col offset="1" span="22">
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="用户名"
          placeholder="请输入用户名(6-18数字字母下划线)"
          :rules="[
          { required: true, message: '请输入用户名(6-18数字字母下划线)' },
          { validator:checkname, message: msgUser }
          ]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          placeholder="请输入密码(6-18数字字母,不能纯数字)"
          :rules="[
          { required: true, message: '请输入密码(6-18数字字母,不能纯数字)' },
          { validator:checkpassword, message: '密码格式不正确' }
          ]"
        />
        <van-field
          v-model="password2"
          type="password"
          name="重复密码"
          placeholder="请输入重复密码"
          :rules="[
            { required: true, message: '请重复输入密码' },
            { validator:checkagain, message: '密码不一致' }
          ]"
        />
        <p class="ty">
          注册账号即表示同意遵守
          <a href="#">《VANCL凡客服务条约》</a>
        </p>
        <div>
          <van-button round block type="info" native-type="submit">注册</van-button>
        </div>
      </van-form>
    </van-col>
  </van-row>
</template>

<script>
import {Toast} from "vant"
import Register from "@/api/register.js";
export default {
  data() {
    return {
      username: "",
      password: "",
      password2: "",
      checkUser: "",
      msgUser: ""
    };
  },
  methods: {
    async checkname(val) {
      try {
        let p = await Register.checkUser(val);
        if (!p.data.flag) {
          if (/^[\da-zA-Z_]{6,18}$/.test(val)) {
            this.checkUser = true;
            return this.checkUser;
          } else {
            this.msgUser = "用户名格式不正确";
            this.checkUser = false;
            return this.checkUser;
          }
        } else {
          this.msgUser = "用户名已存在";
          this.checkUser = false;
          return this.checkUser;
        }
      } catch (err) {
        console.log(err);
      }
    },
    checkpassword(val) {
      return /^(?!\d+$)[\da-zA-Z]{6,18}$/.test(val);
    },
    checkagain(val) {
      if (val != this.password) {
        this.checkUser = false;
        return this.checkUser;
      }
    },
    onSubmit(values) {
      if (this.checkUser) {
        this.reg(this.username, this.password);
      }
    },
    async reg(username, password) {
      try {
        let p = await Register.register(username, password);
        if (p.data.flag) {
          const toast = Toast.loading({
            duration: 0, // 持续展示 toast
            forbidClick: true,
            message: "注册中..."
          });

          let second = 1;
          const timer = setInterval(async () => {
            second--;
            if (second) {
              toast.message = `注册中...`;
            } else {
              clearInterval(timer);
              // 手动清除 Toast
              Toast.clear();
              let res = await this.$store.dispatch("Login", { username, password });
              if (res.flag) {
                const toast = Toast.loading({
                duration: 0, // 持续展示 toast
                forbidClick: true,
                message: '登录中...',
              });

              let second = 1;
              const timer = setInterval(async () => {
                second--;
                if (second) {
                  toast.message = `登录成功`;
                } else {
                  clearInterval(timer);
                  // 手动清除 Toast
                  Toast.clear();
                  this.$router.push("/my/MyCenter");
                let c = await this.$store.dispatch("getcart", username);
                if (!c.flag) {
                  this.$toast.fail("获取失败");
                }
  }
}, 1000);
                
              }
            }
          }, 1000);
        }
      } catch (err) {
        console.log(err);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
::-webkit-input-placeholder {
  color: red;
  font-size: 20px;
  font-weight: bolder;
}
#login1 {
  width: 100%;
  height: 400px;
  background: darkcyan;
}
#username {
  width: 100%;
  height: 30px;
}
.van-button--info {
  background: rgb(184, 28, 34);
  border: none;
  border-radius: 2px;
  margin: 38px 0 10px 0;
}
.van-cell {
  margin: 20px 0;
  background: rgb(245, 245, 245);
}
.regis {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  margin-bottom: 23px;
  a {
    color: gray;
  }
}
.ord_login {
  font-size: 12px;
  color: gray;
}
#Vcode {
  display: flex;
  justify-content: space-between;
  align-items: center;
  img {
    width: 130px;
    height: 42px;
  }
}
.ty {
  color: rgb(172, 171, 171);
  a {
    color: rgb(34, 34, 34);
  }
}
</style>